import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * 双色球程序主类，包含菜单交互（生成5注、兑奖、退出）
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Text1 checker = new Text1();
        int choice;

        do {
            // 显示菜单
            System.out.println("\n===== 双色球程序菜单 =====");
            System.out.println("1. 生成5注彩票号码");
            System.out.println("2. 兑奖");
            System.out.println("3. 退出");
            System.out.print("请选择操作（1-3）：");

            try {
                choice = scanner.nextInt();
                scanner.nextLine(); // 消费换行符，避免后续nextLine()读取到空行

                switch (choice) {
                    case 1:
                        generateFiveTickets(scanner, checker);
                        break;
                    case 2:
                        redeemPrize(scanner, checker);
                        break;
                    case 3:
                        System.out.println("感谢使用，再见！");
                        break;
                    default:
                        System.out.println("无效选择，请重新输入！");
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入有效的数字！");
                scanner.nextLine(); // 清除错误输入
                choice = -1; // 强制进入下一次循环
            }
        } while (choice != 3);

        scanner.close();
    }

    /**
     * 生成5注彩票并展示
     */
    private static void generateFiveTickets(Scanner scanner, Text1 checker) {
        int[][] tickets = Text2.generateFiveTickets();
        System.out.println("\n===== 生成的5注彩票号码 =====");
        for (int i = 0; i < 5; i++) {
            int[] redBalls = new int[6];
            System.arraycopy(tickets[i], 0, redBalls, 0, 6);
            int blueBall = tickets[i][6];
            System.out.println("第" + (i + 1) + "注：" + Text2.formatNumbers(redBalls, blueBall));
        }
    }

    /**
     * 兑奖流程（原交互逻辑）
     */
    private static void redeemPrize(Scanner scanner, Text1 checker) {
        System.out.println("\n请输入您的双色球投注号码（6个红球+1个蓝球，空格分隔，如：01 05 12 18 26 33 09）：");
        String input = scanner.nextLine();

        int[] userNumbers = Text2.parseInput(input);
        if (userNumbers == null) {
            System.out.println("输入格式错误，请按照示例格式输入！");
            return;
        }

        int[] userRedBalls = new int[6];
        System.arraycopy(userNumbers, 0, userRedBalls, 0, 6);
        int userBlueBall = userNumbers[6];

        if (!Text2.validateRedBalls(userRedBalls)) {
            System.out.println("红球号码不合法！红球必须是6个不重复的数字，范围1-33。");
            return;
        }

        if (!Text2.validateBlueBall(userBlueBall)) {
            System.out.println("蓝球号码不合法！蓝球必须是1-16之间的数字。");
            return;
        }

        int[] drawRedBalls = Text2.generateRedBalls();
        int drawBlueBall = Text2.generateBlueBall();

        System.out.println("\n开奖结果：" + Text2.formatNumbers(drawRedBalls, drawBlueBall));

        int prizeLevel = checker.checkPrizeLevel(userRedBalls, userBlueBall, drawRedBalls, drawBlueBall);

        if (prizeLevel == 0) {
            System.out.println("很遗憾，您未中奖。");
        } else {
            System.out.println("恭喜！您中了" + checker.getPrizeName(prizeLevel) + "！");
        }
    }
}